package com.example.noobpackage.logic.repository

import android.util.Log
import androidx.lifecycle.liveData
import com.example.noobpackage.logic.network.LoginService
import com.example.noobpackage.logic.network.ServiceCreator
import kotlinx.coroutines.Dispatchers

object UserRepository {
    private val loginService = ServiceCreator.create<LoginService>()

    fun userLogin(account: Long, password: String) =
        liveData(Dispatchers.IO) {
            try {
                Log.d("AppNetwork", "login start")
                val loginRes = loginService.login(account, password)
                Log.d("AppNetwork", "login success with $loginRes")
                emit(loginRes)
            } catch (e: Throwable) {
                Log.d("AppNetwork", "login error")
                e.printStackTrace()
            }

        }

    fun userRegister(
        account: Long,
        password: String,
        name: String,
        department: Int,
        telephone: String
    ) = liveData(Dispatchers.IO) {
        try {
            Log.d("AppNetwork", "register start")
            val registerRes = loginService.register(account, password, name, department, telephone)
            Log.d("AppNetwork", "register success with $registerRes")
            emit(registerRes)
        } catch (e: Throwable) {
            Log.d("AppNetwork", "register error")
            e.printStackTrace()
        }
    }
}